package my.action.article;

import java.awt.Image;
import java.io.InputStream;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.tomcat.util.http.fileupload.FileItemIterator;
import org.apache.tomcat.util.http.fileupload.FileItemStream;
import org.apache.tomcat.util.http.fileupload.FileUploadException;
import org.apache.tomcat.util.http.fileupload.servlet.ServletFileUpload;

import my.controller.Action;
import my.controller.ActionForward;
import my.db.DAO.ArticleDAO;
import my.db.DAO.ArticleTypeDAO;
import my.db.DTO.ArticleDTO;
import my.db.DTO.ArticleTypeDTO;
import my.db.DTO.AuthorDTO;
import my.db.DTO.FileUpload;

public class AddNewArticleAction implements Action{

	@Override
	public ActionForward execute(HttpServletRequest request,HttpServletResponse response) throws Exception {
		ActionForward forward=new ActionForward();
		ArrayList<String> i_value=new ArrayList<>();
		response.setContentType("text/html");
	    boolean isMultipart=ServletFileUpload.isMultipartContent(request);
	    String image="";
	    if(isMultipart){
	    ServletFileUpload upload=new ServletFileUpload();
	    try {
			FileItemIterator itr=upload.getItemIterator(request);
			while(itr.hasNext()){
				FileItemStream item=itr.next();
				if(item.isFormField()){
					String filename=item.getFieldName();
					InputStream is=item.openStream();
					byte[] bb=new byte[is.available()];
					is.read(bb);
					String values=new String(bb);
					i_value.add(values);
					
				}else{
					String path=(String)request.getAttribute("path");
					System.out.println(path);
					image=item.getName();
					if(FileUpload.processfile(path, item))
						 response.getWriter().println("file uploaded successfully!!!");
					else response.getWriter().println();
				}
			}
			
			HttpSession session=request.getSession(true);
			int userid=(Integer)session.getAttribute("userid");
			
			ArticleTypeDTO atdto=new ArticleTypeDTO();
			atdto.setTypeID(Integer.parseInt(i_value.get(1)));
			
			AuthorDTO audto=new AuthorDTO();
			audto.setAuthorID(userid);
			
			ArticleDTO adto=new ArticleDTO();
			adto.setArticleTitle(i_value.get(0));
			adto.setArticleType(atdto);
			adto.setArticleContent(i_value.get(2));
			adto.setArticleDate(getMachineTime());
			adto.setArticleImage(image);
			adto.setAuthor(audto);
			
			ArticleDAO adao=new ArticleDAO();
			System.out.println(adto.toString());
			if(adao.addArticle(adto)){
				System.out.println("Add Sucessfully!!!");
			}
		} catch (FileUploadException e) {
					e.printStackTrace();
		}
	    		
	}
	    
	    ArticleTypeDAO atdao=new ArticleTypeDAO();
	 	ArrayList<ArticleTypeDTO> lst_atadto=new ArrayList<>();
	 	lst_atadto=atdao.getListArticleType();		 	
	 	request.setAttribute("ArticleTypeNew", lst_atadto);

	 	forward.setRedirect(false);
	 	forward.setPath("./add_new_book.jsp");		 
		return forward;
	}
	public Date getMachineTime(){
		DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd");
		Calendar cal = Calendar.getInstance();
		return cal.getTime();
	}
}
